package com.medishare.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.medishare.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class FragmentQuizResult extends Fragment{

	private Activity mainActivity;
    public final static String  TAG = FragmentQuizResult.class.getSimpleName();
    public final static boolean DEBUG = true;
	private String content;
	private TextView TVName;
	private TextView TVSummary;
	private TextView TVSummary2;
	private GridView mGridView;
	private ArrayList<Score> scores;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		content = getArguments().getString("content");
		View view = inflater.inflate(R.layout.fragment_quiz_result, null);
		TVName = (TextView) view.findViewById(R.id.name);
		TVSummary = (TextView) view.findViewById(R.id.summary);
		TVSummary2 = (TextView) view.findViewById(R.id.summary2);
		mGridView = (GridView) view.findViewById(R.id.grid_result);
		try {
			loadData(content);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return view;
	}
	
	private void loadData(String content) throws JSONException {
		 JSONObject obj = new JSONObject(content);
		 JSONObject objData = obj.getJSONObject("data");
		 JSONArray arrayResult = objData.getJSONArray("result");
		 JSONArray arrayType = objData.getJSONArray("type");
		 String summary = objData.getString("summary");
		 String name = objData.getString("name");
		 String types[] = new String[arrayType.length()];
		 for(int i=0;i<arrayType.length();i++){
			 types[i] = arrayType.getString(i);
		 }
		 scores = new ArrayList<Score>();
		 for(int i=0;i<arrayResult.length();i++){
			 Score score = new Score();
			 JSONObject objTemp = arrayResult.getJSONObject(i);
			 String scoreStr = objTemp.getInt("score") + "";
			 String type = types[objTemp.getInt("type")];
			 score.setScore(scoreStr);
			 score.setType(type);
			 scores.add(score);
		 }
		 TVName.setText(String.format(mainActivity.getResources().getString(R.string.health_quiz_test_name), name));
		 TVSummary.setText(scores.get(0).type + " " + scores.get(0).score);
		 TVSummary2.setText(summary);
		 mGridView.setAdapter(new myAdapter());
	}


	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mainActivity =  activity;
		
	}
	
  
    

	
    class Score{
		private String score;
		private String type;
		public String getScore() {
			return score;
		}
		public void setScore(String score) {
			this.score = score;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		
	}
    
    class myAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			
			return scores.size();
		}

		@Override
		public Score getItem(int position) {
			return scores.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv = new TextView(mainActivity);
			tv.setGravity(Gravity.CENTER);
			tv.setText(getItem(position).getType() + " " + getItem(position).getScore());
			return tv;
		}
    	
    }
}
